package com.yxx.sys.common;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestControllerAdvice
public class CustomExtHandle {


  // 捕获全局异常
  @ExceptionHandler(value = Exception.class)
  public Map handleException(Exception e, HttpServletRequest request) {
    Map<String, Object> map = new HashMap<>();
    map.put("code", 100);
    map.put("msg", e.getMessage());
    map.put("url", request.getRequestURL());
    return map;
  }

//  // 如果是Myexception类
//  @ExceptionHandler(value = AppException.class)
//  Object handleMyException(AppException e, HttpServletRequest request) {
//    ModelAndView modelAndView = new ModelAndView();
//    modelAndView.setViewName("error.html"); // 指定错误跳转页面 需要在templates里面新建 一个error.html
//    modelAndView.addObject("msg", e.getMsg());
//    modelAndView.addObject("code", e.getCode());
//    modelAndView.addObject("url", request.getRequestURL());
//    return modelAndView;
//  }
}